<template>
  <div class="sjplatformApproveForm">
    <el-form label-width="100px">
      <el-form-item label="项目名称：">
        <span>{{ projectStore.requirementModel.projectName }}</span>
      </el-form-item>
      <el-form-item label="需求简介：">
        <span>{{ projectStore.requirementModel.mark }}</span>
      </el-form-item>
    </el-form>
    <div v-for="(requirement, reqIndex) in projectStore.requirementModel.requirements" :key="reqIndex"
      class="sjplatform-form-item">
      <el-form-item :key="requirement.id" :label="'需求' + (reqIndex + 1) + '：'">
        <span>{{ requirement.title }}</span>
      </el-form-item>
      <el-form-item label="描述：">
        <span>{{ requirement.mark }}</span>
      </el-form-item>
      <el-form-item label="父级：" v-if="requirement.sort > 1">
        <el-select v-model="requirement.pId" clearable placeholder="请选择父级功能" disabled>
          <el-option
            v-for="r in projectStore.requirementModel.requirements.filter(r => r.id !== requirement.id && r.sort < requirement.sort && r.title != '')"
            :key="r.id" :label="r.title" :value="r.id" />
        </el-select>
      </el-form-item>

      <!-- 功能 -->
      <div v-for="(item, itemIndex) in requirement.childFeatures" :key="itemIndex" class="form-child">
        <el-form-item :key="item.id" :label="'功能' + (itemIndex + 1) + '：'">
          <span>{{ item.title }}</span>
        </el-form-item>
        <el-form-item label="描述：">
          <span>{{ item.mark }}</span>
        </el-form-item>
        <el-form-item label="父级：" v-if="item.sort > 1">
          <el-select v-model="item.pId" clearable placeholder="请选择父级功能" disabled>
            <el-option
              v-for="f in requirement.childFeatures.filter(f => f.id !== item.id && f.sort < item.sort && f.title != '')"
              :key="f.id" :label="f.title" :value="f.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="类型：">
          <el-select v-model="item.type" clearable placeholder="请选择功能类型" disabled>
            <el-option v-for="r in projectStore.requirementType" :key="r.value" :label="r.label" :value="r.value" />
          </el-select>
        </el-form-item>
        <!--接口-->
        <template v-if="item.type == 2">
          <el-form-item label="参数：">
            <span>{{ item.parameters }}</span>
          </el-form-item>
          <el-form-item label="返回值：">
            <span>{{ item.rValue }}</span>
          </el-form-item>
        </template>
        <!--列表-->
        <template v-if="item.type == 3">
          <el-form-item label="列名：">
            <span>{{ item.columns }}</span>
          </el-form-item>
        </template>
      </div>
    </div>
    <el-form label-width="120px" v-if="historyFileList.length > 0">
      <el-form-item label="需求文档：">
        <div class="uploadHis">
          <ul class="file-list">
            <li v-for="file in historyFileList" :key="file.objectId">
              {{ file.fileName }}
              <el-tooltip content="下载" placement="bottom" effect="light">
                <i class="download-icon" @click="fileOpeartion.downloadFile(file.objectId, file.fileName)"></i>
              </el-tooltip>
            </li>
          </ul>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { ref, defineProps, defineExpose } from 'vue';
import { useProjectTable } from "../../../hooks/ProjectManager/Start/useProjectTable";
import { useProjectRequirement } from "../../../hooks/ProjectManager/Plan/useProjectRequirement";
import { useFileOperation } from '../../../hooks/Common/useFileOperation';

let fileOpeartion = useFileOperation();
let receivedProps = defineProps(['requirementId']);
let { projectStore, getRequirementDetailById, updateRequirementMainSpecificContent } = useProjectRequirement(1, 10);
let { projectTable, getProjectDoc } = useProjectTable(1, 10);

let historyFileList = ref([]);

getRequirementDetailById(receivedProps.requirementId).then(() => {
  //获取需求文档
  getProjectDoc(projectStore.requirementModel.projectId, 2).then(() => {
    historyFileList.value = projectTable.documentList;
  });
})

//更新审批状态
async function updateApproveStatus(value) {
  projectStore.requirementModel.id = receivedProps.requirementId;
  //评审状态更新
  if (!projectStore.requirementModel.updateProperties) {
    projectStore.requirementModel.updateProperties = [];
  }
  projectStore.requirementModel.status = value;
  projectStore.requirementModel.updateProperties.push('Status');
  await updateRequirementMainSpecificContent();
}

defineExpose({ updateApproveStatus });
</script>